package com.farm.kaiping.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.farm.common.security.SecurityUtils;
import com.farm.kaiping.entity.SysMenu;
import com.farm.kaiping.entity.SysRole;
import com.farm.kaiping.entity.SysRoleMenu;
import com.farm.kaiping.mapper.SysMenuMapper;
import com.farm.kaiping.po.SysRoleMenuPO;
import com.farm.kaiping.po.SysRolePO;
import com.farm.kaiping.mapper.SysRoleMapper;
import com.farm.kaiping.service.SysRoleMenuService;
import com.farm.kaiping.service.SysRoleService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.farm.common.util.StringRange;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 系统角色表(SysRole)表服务实现类
 *
 * @author zhuhui
 * @since 2024-11-26 23:18:23
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

	private final SysRoleMenuService sysRoleMenuService;
	private final SysMenuMapper sysMenuMapper;
    
	private LambdaQueryWrapper<SysRole> generateQueryWrapper(SysRolePO sysRolePO){
		LambdaQueryWrapper<SysRole> queryWrapper = new LambdaQueryWrapper<>();
		// 角色id
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getRoleId()), SysRole::getRoleId, sysRolePO.getRoleId());
		queryWrapper.in(ObjectUtil.isNotEmpty(sysRolePO.getRoleIdList()), SysRole::getRoleId, sysRolePO.getRoleIdList());
		// 角色名称
		queryWrapper.like(ObjectUtil.isNotEmpty(sysRolePO.getRoleName()), SysRole::getRoleName, sysRolePO.getRoleName());
		// 角色标志
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getRoleCode()), SysRole::getRoleCode, sysRolePO.getRoleCode());
		queryWrapper.in(ObjectUtil.isNotEmpty(sysRolePO.getRoleCodeList()), SysRole::getRoleCode, sysRolePO.getRoleCodeList());
		// 描述
		queryWrapper.like(ObjectUtil.isNotEmpty(sysRolePO.getRoleDesc()), SysRole::getRoleDesc, sysRolePO.getRoleDesc());
		// 是否删除
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getDelFlag()), SysRole::getDelFlag, sysRolePO.getDelFlag());
        queryWrapper.eq(ObjectUtil.isEmpty(sysRolePO.getDelFlag()), SysRole::getDelFlag, 0);
		// 新增时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getCreateTime()), SysRole::getCreateTime, sysRolePO.getCreateTime());
		// 创建人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getCreateUserId()), SysRole::getCreateUserId, sysRolePO.getCreateUserId());
		// 创建人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(sysRolePO.getCreateUserName()), SysRole::getCreateUserName, sysRolePO.getCreateUserName());
		// 更新时间
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getUpdateTime()), SysRole::getUpdateTime, sysRolePO.getUpdateTime());
		// 更新人ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getUpdateUserId()), SysRole::getUpdateUserId, sysRolePO.getUpdateUserId());
		// 更新人姓名
		queryWrapper.like(ObjectUtil.isNotEmpty(sysRolePO.getUpdateUserName()), SysRole::getUpdateUserName, sysRolePO.getUpdateUserName());
		// 租户ID
		queryWrapper.eq(ObjectUtil.isNotEmpty(sysRolePO.getTenantId()), SysRole::getTenantId, sysRolePO.getTenantId());
		// 创建时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(sysRolePO.getCreateTimeBetween(),0)), SysRole::getCreateTime,StringRange.getOne(sysRolePO.getCreateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(sysRolePO.getCreateTimeBetween(),1)), SysRole::getCreateTime,StringRange.getOne(sysRolePO.getCreateTimeBetween(),1));
		// 更新时间时间区间查询
		queryWrapper.ge(ObjectUtil.isNotEmpty(StringRange.getOne(sysRolePO.getUpdateTimeBetween(),0)), SysRole::getUpdateTime,StringRange.getOne(sysRolePO.getUpdateTimeBetween(),0));
		queryWrapper.le(ObjectUtil.isNotEmpty(StringRange.getOne(sysRolePO.getUpdateTimeBetween(),1)), SysRole::getUpdateTime,StringRange.getOne(sysRolePO.getUpdateTimeBetween(),1));

		queryWrapper.orderByDesc(SysRole::getCreateTime,SysRole::getRoleId);
		return queryWrapper;
	}

	 /**
	  * @describe 分页查询系统角色表
	  * @author zhuhui
	  * @date 2024-11-26 23:18:23
	  */
	@Override
	public Page<SysRole> getSysRolePage(Page page, SysRolePO sysRolePO){
		LambdaQueryWrapper<SysRole> queryWrapper = this.generateQueryWrapper(sysRolePO);
        return this.baseMapper.selectPage(page, queryWrapper);
	}
	
	/**
     * @describe 查询系统角色表list
     * @author  zhuhui
     * @date  2024-11-26 23:18:23
     */
	@Override
	public List<SysRole> getSysRoleList(SysRolePO sysRolePO){
		LambdaQueryWrapper<SysRole> queryWrapper = this.generateQueryWrapper(sysRolePO);
        return this.baseMapper.selectList(queryWrapper);
	}
	
	/**
     * @describe 获取一条数据 系统角色表
     * @author  zhuhui
     * @date  2024-11-26 23:18:23
     */
	@Override
	public SysRole getSysRole(SysRolePO sysRolePO){
		LambdaQueryWrapper<SysRole> queryWrapper = this.generateQueryWrapper(sysRolePO);
        queryWrapper.last("LIMIT 1");
        return this.baseMapper.selectOne(queryWrapper);
	}

    /**
     * @describe 新增系统角色表
     * @author  zhuhui
     * @date  2024-11-26 23:18:23
     */
	@Override
	public Integer saveSysRole(SysRolePO sysRolePO) {
		if (ObjectUtil.isEmpty(sysRolePO.getRoleName())){
			throw new RuntimeException("角色名称为空");
		}
		if (ObjectUtil.isEmpty(sysRolePO.getRoleCode())){
			throw new RuntimeException("角色标志为空");
		}

		LambdaQueryWrapper<SysRole> existQueryWrapper = new LambdaQueryWrapper<>();
		existQueryWrapper.eq(SysRole::getRoleCode, sysRolePO.getRoleCode());
		existQueryWrapper.eq(SysRole::getDelFlag, 0);
		if (this.baseMapper.selectCount(existQueryWrapper) > 0){
			throw new RuntimeException("角色标志已存在");
		}

        sysRolePO.setDelFlag(0);
		sysRolePO.setTenantId(SecurityUtils.getUser().getTenantId());// 租户ID
		sysRolePO.setCreateTime(LocalDateTime.now());
		sysRolePO.setCreateUserId(SecurityUtils.getUser().getUserId());// 创建人
		sysRolePO.setCreateUserName(SecurityUtils.getUser().getRealName());// 创建人
		sysRolePO.setUpdateTime(LocalDateTime.now());
		SysRole sysRole = BeanUtil.copyProperties(sysRolePO, SysRole.class);
		this.save(sysRole);
		return sysRole.getRoleId();
	}

	/**
     * @describe 修改系统角色表
     * @author  zhuhui
     * @date  2024-11-26 23:18:23
     */
	@Override
	public String updateSysRole(SysRolePO sysRolePO) {
		if (ObjectUtil.isEmpty(sysRolePO.getRoleId())) {
			throw new RuntimeException("id为空");
		}

		if (ObjectUtil.isEmpty(sysRolePO.getRoleName())){
			throw new RuntimeException("角色名称为空");
		}
		if (ObjectUtil.isEmpty(sysRolePO.getRoleCode())){
			throw new RuntimeException("角色标志为空");
		}

		LambdaQueryWrapper<SysRole> existQueryWrapper = new LambdaQueryWrapper<>();
		existQueryWrapper.eq(SysRole::getRoleCode, sysRolePO.getRoleCode());
		existQueryWrapper.eq(SysRole::getDelFlag, 0);
		existQueryWrapper.ne(SysRole::getRoleId, sysRolePO.getRoleId());
		if (this.baseMapper.selectCount(existQueryWrapper) > 0){
			throw new RuntimeException("角色标志已存在");
		}

		sysRolePO.setUpdateUserId(SecurityUtils.getUser().getUserId());// 更新人
		sysRolePO.setUpdateUserName(SecurityUtils.getUser().getRealName());// 更新人
		sysRolePO.setUpdateTime(LocalDateTime.now());
		SysRole sysRole = BeanUtil.copyProperties(sysRolePO, SysRole.class);
		this.updateById(sysRole);
		return "修改成功";
	}

	/**
     * @describe 通过id删除系统角色表
     * @author  zhuhui
     * @date  2024-11-26 23:18:23
     */
	@Override
	public String removeSysRole(SysRolePO sysRolePO) {
		if (ObjectUtil.isEmpty(sysRolePO.getBatchDel())) {
			throw new RuntimeException("删除数据未选择");
		}
		LambdaUpdateWrapper<SysRole> removeWrapper = new LambdaUpdateWrapper<>();
		removeWrapper.in(SysRole::getRoleId, sysRolePO.getBatchDel());
		removeWrapper.set(SysRole::getDelFlag,1);
		removeWrapper.set(SysRole::getUpdateUserId,SecurityUtils.getUser().getUserId());// 更新人
		removeWrapper.set(SysRole::getUpdateUserName,SecurityUtils.getUser().getRealName());// 更新人
		removeWrapper.set(SysRole::getUpdateTime,LocalDateTime.now());

		this.update(removeWrapper);
		return "删除成功";
	}

	@Override
	public String removeSysRoleMenu(SysRoleMenuPO sysRoleMenuPO) {
		return sysRoleMenuService.removeSysRoleMenu(sysRoleMenuPO);
	}

	@Override
	public String saveSysRoleMenu(SysRoleMenuPO sysRoleMenuPO) {
		return sysRoleMenuService.saveSysRoleMenu(sysRoleMenuPO);
	}

	@Override
	public List<SysMenu> getSysRoleMenu(Integer roleId) {
		List<Integer> sysRoleMenuId = this.getSysRoleMenuId(roleId);
		if (ObjectUtil.isNotEmpty(sysRoleMenuId)){
			LambdaQueryWrapper<SysMenu> sysMenuLambdaQueryWrapper = new LambdaQueryWrapper<>();
			sysMenuLambdaQueryWrapper.in(SysMenu::getMenuId, sysRoleMenuId);
			return sysMenuMapper.selectList(sysMenuLambdaQueryWrapper);
		}
		return null;
	}

	@Override
	public List<Integer> getSysRoleMenuId(Integer roleId) {
		if (ObjectUtil.isEmpty(roleId)){
			throw new RuntimeException("角色id为空");
		}
		SysRoleMenuPO sysRoleMenuPO = new SysRoleMenuPO();
		sysRoleMenuPO.setRoleId(roleId);
		List<SysRoleMenu> sysRoleMenuList = sysRoleMenuService.getSysRoleMenuList(sysRoleMenuPO);
		List<Integer> menuIds = sysRoleMenuList.stream().map(SysRoleMenu::getMenuId).collect(Collectors.toList());
		if (ObjectUtil.isNotEmpty(menuIds)){
			return menuIds;
		}
		return null;
	}

}

